var tools = require('../system/tools')

var authRules = {
    'getUserInfo': 'scope.userInfo'
}

var privateObj = {
    isLogin: function () {
        try {
            return cache.get(CK_ACCOUNT_LOGIN_FLAG)
        } catch (e) {
            console.log('is login chk error')
            console.log(e)
        }
        return ''
    },
    login: function (userToken) {
        try {
            return cache.set(CK_ACCOUNT_LOGIN_FLAG, userToken)
            //return wx.setStorageSync(CK_ACCOUNT_LOGIN_FLAG, userToken)
        } catch (e) {
            console.log(e)
        }
    }
}

var wechat = {
    call: function (func, closure) {
        var authScope = '';
        if (!authRules[func]) {
            closure()
        } else {
            authScope = authRules[func];
            wx.getSetting({
                success: res => {
                    if (res.authSetting[authScope]) {
                        closure()
                    } else {
                        wx.authorize({
                            scope: authScope,
                            success() {
                                closure()
                            }
                        })
                    }
                }
            })
        }
    }
}

module.exports = wechat